/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2007 Copyright (C)
 *
 *  File: mp_subtitle_smi.c
 *
 *  Description: This file describes smi file operations.
 *
 *  History:
 *      Date        	Author      	Version  		Comment
 *      ====        	======      =======  	=======
 *  1.  2009.8.25  Martin_Xia  	0.0.1  		
 *
 ****************************************************************************/

#include <types.h>
#include <retcode.h>
#include <api/libc/printf.h>

#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libfs2/stat.h>
#include <api/libfs2/dirent.h>
#include <api/libfs2/statvfs.h>
#include <api/libfs2/unistd.h>
#include <api/libfs2/stdio.h>

#include <api/libmp/media_player_api.h>
#include <api/libmp/mp_subtitle_api.h>
#include "mp_subtitle_smi.h"
#include "mp_subtitle_vobsub.h"
#include "mp_subtitle.h"



/**********************Global  Variables***********************************/
//define the Global  Variables


/**************************Static  Function Declare***********************************/
static void mp_subtitle_parse_utf8_smi(MpSubtitle *p, char *str, unsigned long *pos, unsigned long *time);
static void mp_subtitle_parse_unicode_smi(MpSubtitle *p, wchar_t *str, unsigned long *pos, unsigned long *time);


#if 0
#endif

/****************************API  Function ***********************************/
/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_smi
//
// Description:
//	API function. Parse smi subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a smi file in memory
//	pos - Store every offset for each subtitle in a smi file
//	time - Store every time for each subtitle in a smi file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////

void mp_subtitle_parse_smi(void *sub, char *str, unsigned long *pos, unsigned long *time)
{	
	MpSubtitle				*p;
	
	MP_SUB_DEBUG("====>>mp_subtitle_parse_smi()\n");

	if ((!sub)||(!str)||(!pos)||(!time))
	{
		MP_SUB_DEBUG("mp_subtitle_parse_smi: Invalid Parameters!\n");
		MP_SUB_DEBUG("<<====mp_subtitle_parse_smi()\n");
		return;
	}


	p = (MpSubtitle*)sub;

	if (p->avail_sub_files[p->current_sub_file_id].avail_file_type != MP_SUBTITLE_TYPE_SMI)
	{
		MP_SUB_DEBUG("mp_subtitle_parse_smi: The subtitle file is not a smi file!\n");
		MP_SUB_DEBUG("<<====mp_subtitle_parse_smi()\n");
		return;
	}

	

	if (p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_L_UNICODE)
	{
		mp_subtitle_parse_unicode_smi(p, (wchar_t*)str, pos, time);
	}
	else if ((p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_UTF8) || (p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_ANSI))
	{
		mp_subtitle_parse_utf8_smi(p, str, pos, time);
	}

	
	MP_SUB_DEBUG("<<====mp_subtitle_parse_smi()\n");
	return;
}



#if 0
#endif

/**************************Internal Static  Function ***********************************/
/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_smi
//
// Description:
//	Parse utf8 smi subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a smi file in memory
//	pos - Store every offset for each subtitle in a smi file
//	time - Store every time for each subtitle in a smi file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////

static void mp_subtitle_parse_utf8_smi(MpSubtitle *p, char* str, unsigned long* pos, unsigned long* time)
{
	char *bol=str;
	char *bot=0,*eot=0,*cot=0;
	unsigned long *spos,*stime;
	long l=0;
	long long realtime;

	MP_SUB_DEBUG("====>>mp_subtitle_parse_utf8_smi()\n");

	spos=pos;
	stime=time;
	while (*bol)
	{
		if (*bol=='\r' || *bol=='\n' || *bol=='\t') *bol=' ';
		if (*bol=='&' && *(bol+1)=='n' && *(bol+2)=='b' && *(bol+3)=='s' && *(bol+4)=='p' && *(bol+5)==';')
		{
			*bol=*(bol+1)=*(bol+2)=*(bol+3)=*(bol+4)=*(bol+5)=' ';
		}
		if (*bol=='<') {bot=bol; eot=0;}
		if (*bol=='>') eot=bol;
		if (bot && eot)
		{
			if 
			(
			(*(bot+1)=='b' || *(bot+1)=='B') &&
			(*(bot+2)=='r' || *(bot+2)=='R')
			)
			{
				//*bot='|';
				*bot = '\n';
				bot++;
			}
			else while 
			(
			(*(bot+1)=='s' || *(bot+1)=='S') &&
			(*(bot+2)=='y' || *(bot+2)=='Y') &&
			(*(bot+3)=='n' || *(bot+3)=='N') &&
			(*(bot+4)=='c' || *(bot+4)=='C') &&
			(*(bot+5)==' ' || *(bot+5)==' ')
			)
			{
				*bot='\0';
				bot++;
				cot=bot;
				while (*cot!='=' && cot<eot) cot++;
				if (*cot!='=') break;
				cot++;
				if (l>=MP_SUBTITLE_MAX_SUB_COUNT-1) break;
				realtime=ATOI(cot);
				realtime*=TICKSPERSEC;
				//realtime/=1000;
				*time=(unsigned int)realtime;
				*pos=bot-str;
				time++;
				pos++;
				l++;
				break;
			}
			while (bot!=eot+1) {*bot=' '; bot++;}
			bot=eot=0;
		}
		bol++;
	}
	*pos=0xFFFFFFFF;
	*time=0;
	l++;
	p->total_sub_count=l;
	MP_SUB_DEBUG("<<====mp_subtitle_parse_utf8_smi()\n");
}

/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_unicode_smi
//
// Description:
//	Parse unicode smi subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a smi file in memory
//	pos - Store every offset for each subtitle in a smi file
//	time - Store every time for each subtitle in a smi file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////

static void mp_subtitle_parse_unicode_smi(MpSubtitle *p, wchar_t *str, unsigned long *pos, unsigned long *time)
{
	wchar_t *bol=str;
	wchar_t *bot=0,*eot=0,*cot=0;
	unsigned long *spos,*stime;
	long l=0;
	long len;
	char c;
	long long realtime;

	MP_SUB_DEBUG("====>>mp_subtitle_parse_utf8_smi()\n");

	spos=pos;
	stime=time;
	while (*bol)
	{
		if (*bol==L'\r' || *bol==L'\n' || *bol==L'\t') *bol=L' ';
		if (*bol==L'&' && *(bol+1)==L'n' && *(bol+2)==L'b' && *(bol+3)==L's' && *(bol+4)==L'p' && *(bol+5)==L';')
		{
			*bol=*(bol+1)=*(bol+2)=*(bol+3)=*(bol+4)=*(bol+5)=L' ';
		}
		if (*bol==L'<') {bot=bol; eot=0;}
		if (*bol==L'>') eot=bol;
		if (bot && eot)
		{
			if 
			(
			(*(bot+1)==L'b' || *(bot+1)==L'B') &&
			(*(bot+2)==L'r' || *(bot+2)==L'R')
			)
			{
				//*bot=L'|';
				*bot = L'\n';
				bot++;
			}
			else while 
			(
			(*(bot+1)==L's' || *(bot+1)==L'S') &&
			(*(bot+2)==L'y' || *(bot+2)==L'Y') &&
			(*(bot+3)==L'n' || *(bot+3)==L'N') &&
			(*(bot+4)==L'c' || *(bot+4)==L'C') &&
			(*(bot+5)==L' ' || *(bot+5)==L' ')
			)
			{
				*bot=L'\0';
				bot++;
				cot=bot;
				while (*cot!=L'=' && cot<eot) cot++;
				if (*cot!=L'=') break;
				cot++;
				if (l>=MP_SUBTITLE_MAX_SUB_COUNT-1) break;
				
				len = 0;
				realtime = 0;
				while (cot[len])
				{
					c = (unsigned char)(cot[len]>> 8);
					if(c>='0' && c<='9')
					{
						realtime = realtime*10 + c - '0';
					}
					else
					{
						break;
					}
					++len;
				}
				
				realtime*=TICKSPERSEC;
				//realtime/=1000;
				*time=(unsigned int)realtime;
				*pos=bot-str;
				time++;
				pos++;
				l++;
				break;
			}
			while (bot!=eot+1) {*bot=L' '; bot++;}
			bot=eot=0;
		}
		bol++;
	}
	*pos=0xFFFFFFFF;
	*time=0;
	l++;
	p->total_sub_count=l;
	MP_SUB_DEBUG("<<====mp_subtitle_parse_utf8_smi()\n");
}

